import { request } from "../request";

// 获取token



// 创建考试参数接口
export interface CreateExamParams {
  teacherId?: number;
  title?: string;
  description?: string;
  startTime?: string;
  endTime?: string;
  duration?: number;
  classIds?: number[]; // 改为支持多个班级ID的数组
}

// 创建考试返回数据接口
export interface CreateExamRes {
  code: number;
  message: string;
  data: Record<string, unknown>;
  timestamp: Record<string, unknown>;
}

// 创建考试
const createExamAPI = async (params: CreateExamParams): Promise<CreateExamRes> => {
  const token = localStorage.getItem("token");
  return request("/api/exams/create", {
    method: "post",
    headers: {
      "Content-Type": "application/json",
      "Authorization": `Bearer ${token}`,
    },
    data: JSON.stringify(params),
  });
};

// 发布考试参数接口
export interface PublishExamParams {
  teacherId: number;  // 改为必需字段
  classIds: number[]; // 改为必需字段，确保总是有值
}

// 发布考试返回数据接口
export interface PublishExamRes {
  code: number;
  message: string;
  data: Record<string, unknown>;
  timestamp: Record<string, unknown>;
}

// 发布考试
const publishExamAPI = async (examId: number, params: PublishExamParams): Promise<PublishExamRes> => {
  const token = localStorage.getItem("token");
  return request(`/api/exams/${examId}/publish`, {
    method: "post",
    headers: {
      "Content-Type": "application/json",
      "Authorization": `Bearer ${token}`,
    },
    data: JSON.stringify(params),
  });
};

export { createExamAPI, publishExamAPI };